﻿using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using WMS.Common;
using WMS.ProductDetails.Dto;

namespace WMS.ProductDetails
{
	public class ProductDetailsAppService : IProductDetailsAppService
	{
		private readonly IRepository<ProductDetailsFiles.ProductDetails, int> _productDetailsRepository;

		public ProductDetailsAppService(IRepository<ProductDetailsFiles.ProductDetails, int> productDetailsRepository)
		{
			_productDetailsRepository = productDetailsRepository;
		}

		public async Task<PagedResult> GetProductDetailsList(int page, int limit, int belongsOwner)
		{
			var predicate = PredicateBuilder.New<ProductDetailsFiles.ProductDetails>(true);
			if (belongsOwner != -1)
			{
				predicate = predicate.And(m => m.BelongsOwner == belongsOwner);
			}
			var productDetailsList = await _productDetailsRepository.GetListAsync(predicate);
			PagedResult pageResult = new PagedResult();
			pageResult.RowCount = productDetailsList.Count();
			pageResult.PageCount = (int)(pageResult.RowCount * 1.0) / limit;
			pageResult.Queryable = productDetailsList.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).AsQueryable();
			return pageResult;
		}

		public async Task<int> AddProductDetails(ProductDetailsFiles.ProductDetails productDetailsModel)
		{
			await _productDetailsRepository.InsertAsync(productDetailsModel);
			return 1;
		}

		public async Task<int> RemoveProductDetails(int id)
		{
			await _productDetailsRepository.DeleteAsync(id);
			return id;
		}

		public async Task<ProductDetailsFiles.ProductDetails> FindProductDetails(int id)
		{
			ProductDetailsFiles.ProductDetails productDetailsModel = await _productDetailsRepository.FindAsync(id);
			return productDetailsModel;
		}

		public async Task<int> UpdateProductDetails(UpdateProductDetails productDetailsModel)
		{
			var dtoModel = productDetailsModel.MapTo<ProductDetailsFiles.ProductDetails>();
			await _productDetailsRepository.UpdateAsync(dtoModel);
			return 1;
		}

		public async Task<int> RemoveRangeProducctDetails(string ids)
		{
			//var entities = await _productDetailsRepository.GetAsync(ids);

			await _productDetailsRepository.DeleteAsync(m => ids.Contains(m.Id.ToString()));
			return 1;
		}







	}
}
